import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class DataPointManager_deprecated {

	ArrayList<ArrayList<String>> forecastData;
	ArrayList<ArrayList<String>> powerOutputData;
	private final int FORECAST_COLUMN = 1;
	private final int FORECASTED_COLUMN = 3;
	private final int FORECAST_HOUR_OFFSET = 4;

	/**
	 * Constructor
	 * @param powerOutputFileName
	 * @param windOutputFileName
	 * @throws IOException
	 */
	public DataPointManager_deprecated(String powerOutputFileName, String windOutputFileName) throws IOException {

		forecastData = CSVIO.readFile(windOutputFileName);
		powerOutputData = CSVIO.readFile(powerOutputFileName);
	}

	/**
	 * Unimplemented
	 * @param startTime
	 * @param endTime
	 * @param windState
	 * @return
	 */
	public ArrayList<DataPoint> getDataPoints(Date startTime, Date endTime, int windState) {
		ArrayList<DataPoint> dataPointList = new ArrayList<DataPoint>();
		return dataPointList;
	}

	/**
	 * Generate and return the map of datapoints from wind output and power output files
	 * @return
	 */
	public TreeMap<Date, DataPoint> getDataPoints() {
		TreeMap<Date, DataPoint> dataPointMap = new TreeMap<Date, DataPoint>();
		int dataPointIDCounter = 0;
		SimpleDateFormat windForecastDateFormat = new SimpleDateFormat("M/d/yyyy");
		SimpleDateFormat powerOutputDateFormat = new SimpleDateFormat("M/d/yy H:mm");
		//go through each row of forecast spreadshet
		for (int forecastRowIndex = 0; forecastRowIndex < forecastData.size(); forecastRowIndex++) {
			ArrayList<String> forecastRow = forecastData.get(forecastRowIndex);

			try {//Use the data only if forecasted day is 1 day ahead of forecast day
				Date forecastDate = windForecastDateFormat.parse(forecastRow.get(FORECAST_COLUMN));
				Date forecastedDate = windForecastDateFormat.parse(forecastRow.get(FORECASTED_COLUMN));
				long diff = (forecastedDate.getTime() - forecastDate.getTime()) / (60 * 60 * 1000);
				if (diff == 24) {
					System.out.print("Forecast date:" +forecastDate + "  Forecasted date:" + forecastedDate + " ");
					//Grab entry from each hour and load into the map
					for (int hour = FORECAST_HOUR_OFFSET; hour < 23 + FORECAST_HOUR_OFFSET; hour++) {
						Date forecastedDateTime = (Date) forecastedDate.clone();
						forecastedDateTime.setHours(hour - FORECAST_HOUR_OFFSET);
						dataPointMap.put(forecastedDateTime, new DataPoint(dataPointIDCounter, forecastedDateTime,
								forecastDate, Double.parseDouble(forecastRow.get(hour))));
						dataPointIDCounter++;
						System.out.print(" " + Double.parseDouble(forecastRow.get(hour)));
					}
					System.out.println();
				}
			} catch (Exception e) {

			}

		}

		// Go through the power output spread sheet and add power outputs to wind datapoints of the same time
		//currently, there are 6 power outputs in an hour, they all go into a datapoint that spans an hour
		for (int powerOutputRowIndex = 0; powerOutputRowIndex < powerOutputData.size(); powerOutputRowIndex++) {
			ArrayList<String> powerOutputRow = powerOutputData.get(powerOutputRowIndex);
			for (int powerOutputColIndex = 0; powerOutputColIndex < powerOutputRow.size(); powerOutputColIndex++) {
				try {
					Date measuredDate = powerOutputDateFormat.parse(powerOutputRow.get(powerOutputColIndex));
					double powerOutput = Double.parseDouble(powerOutputData.get(powerOutputRowIndex + 1).get(
							powerOutputColIndex));
					Date flooredMeasuredDate = (Date) measuredDate.clone();
					flooredMeasuredDate.setMinutes(0);
					//get the datapoint upto only hour resolution(ignore the minutes)
					DataPoint dp = dataPointMap.get(flooredMeasuredDate);
					dp.addToPowerOutputList(powerOutput);
				} catch (Exception e) {

				}
			}
		}

		return dataPointMap;
	}

}
